﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Hexagonal;

namespace GUIComponents
{
    public partial class MapPanel : UserControl
    {
        #region Attributes
        private Hexagonal.Board board;
        private Hexagonal.GraphicsEngine graphicsEngine;

        private const int MAP_SIZE_ROWS = 70;
        private const int MAP_SIZE_COLS = 70;
        private const int HEX_SIDE = 25;
        private double HEX_H_VALUE = Hexagonal.Math.CalculateH(HEX_SIDE);
        private double HEX_R_VALUE = Hexagonal.Math.CalculateR(HEX_SIDE);
        private int HEX_WIDTH = HEX_SIDE + (int)Hexagonal.Math.CalculateH(HEX_SIDE);
        private int HEX_HEIGHT = (int)(2 * Hexagonal.Math.CalculateR(HEX_SIDE));

        private int mapId;
        #endregion

        #region Properties
        public int HexWidth
        {
            get { return HEX_WIDTH; }
        }
        public int HexHeight
        {
            get { return HEX_HEIGHT; }
        }
        public double HexHValue
        {
            get { return HEX_H_VALUE; }
        }
        public double HexRValue
        {
            get { return HEX_R_VALUE; }
        }
        public int MapRows
        {
            get { return MAP_SIZE_ROWS; }
        }
        public int MapColumns
        {
            get { return MAP_SIZE_COLS; }
        }

        public int MapID
        {
            get { return mapId; }
            set { mapId = value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public MapPanel()
        {
            InitializeComponent();

            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint, true);

            board = new Board(MAP_SIZE_ROWS, MAP_SIZE_COLS, HEX_SIDE, HexOrientation.Flat);

            board.BoardState.BackgroundColor = Color.Green;
            board.BoardState.GridPenWidth = Convert.ToInt32(2);
            board.BoardState.ActiveHexBorderColor = Color.Red;
            board.BoardState.ActiveHexBorderWidth = Convert.ToInt32(3);

            this.graphicsEngine = new GraphicsEngine(board, Application.StartupPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MapPanel_Paint(object sender, PaintEventArgs e)
        {
            if (graphicsEngine != null)
            {
                graphicsEngine.DrawHexBoard(e.Graphics);
                graphicsEngine.DrawHexMapContents(e.Graphics, mapId);
            }
        }

        private void MapPanel_MouseClick(object sender, MouseEventArgs e)
        {
            if (board != null && graphicsEngine != null)
            {
                //
                // need to account for any offset
                //
                Point mouseClick = new Point(e.X - graphicsEngine.BoardXOffset,
                                             e.Y - graphicsEngine.BoardYOffset);
                Hex clickedHex = board.FindHexMouseClick(mouseClick);
                if (clickedHex == null)
                {
                    board.BoardState.ActiveHex = null;
                }
                else
                {
                    board.BoardState.ActiveHex = clickedHex;
                    if (e.Button == MouseButtons.Right)
                    {
                        clickedHex.HexState.BackgroundColor = Color.Blue;
                    }
                }

                this.Invalidate();
            }
        }
    }
}
